package com.nvn.washiodriver.activities;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.nvn.washiodriver.R;
import com.nvn.washiodriver.Objects.MapIntent;
import com.nvn.washiodriver.views.CustomDialog;

public class SettingsActivity extends BaseActivity implements OnClickListener {

	private TextView mTvMapPre;
	private CustomDialog myDialogMaps;
	private int mPosMap;
	private SharedPreferences pref;
	private Editor edt;
	private ArrayList<Object> arr;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settings);

		initView();
		initProperties();

	}

	@Override
	protected void initView() {
		super.initView();
		mTvMapPre = (TextView) findViewById(R.id.tv_map_pre);
	}

	private void initProperties() {
		pref = getSharedPreferences("WASHIO_DRIVER", MODE_PRIVATE);
		edt = pref.edit();

		mTvMapPre.setOnClickListener(this);
		getMapItent();

		mPosMap = pref.getInt("POS_MAP", 0);
		mTvMapPre.setText(((MapIntent) arr.get(mPosMap)).getName());
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.tv_map_pre) {
			showDialogMaps();
		}
	}

	public void showDialogMaps() {

		myDialogMaps = new CustomDialog(this, arr, getResources().getString(
				R.string.txt_map_type), mPosMap);
		myDialogMaps.setOnClickItem(new CustomDialog.OnClickItem() {

			@Override
			public void Click(int position) {
				// TODO Auto-generated method stub
				position -= 1;
				mTvMapPre.setText(arr.get(position).toString());
				mPosMap = position;

				edt.putInt("POS_MAP", position);
				edt.commit();
			}
		});
		myDialogMaps.show();
	}

	public void getMapItent() {
		arr = new ArrayList<Object>();
		PackageManager pm = getPackageManager();
		Intent sendIntent = new Intent(android.content.Intent.ACTION_VIEW,
				Uri.parse("http://maps.google.com/maps?saddr= " + 0l + "," + 0l
						+ "&daddr=" + 0l + "," + 0l + ""));
		List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0);
		for (int i = 0; i < resInfo.size(); i++) {
			ResolveInfo ri = resInfo.get(i);
			Intent intent = new Intent();
			intent.setClassName(ri.activityInfo.packageName,
					ri.activityInfo.name);
			// MapIntent item = new MapIntent(ri.loadLabel(getPackageManager())
			// .toString(), intent);
			// arr.add(item);
		}

		arr.add(new MapIntent("Google Maps", ""));
		arr.add(new MapIntent("Apple Maps", ""));
		arr.add(new MapIntent("Waze", ""));

	}
}
